package com.jcfk.eam.service.equ;

import com.jcfk.common.business.service.BaseService;
import com.jcfk.eam.dao.main.EquStatisticsRepairOrderDao;
import com.jcfk.eam.domain.dto.main.EChartData;
import com.jcfk.eam.domain.dto.main.EChartDataItem;
import com.jcfk.eam.domain.dto.main.EquStatisticsDTO;
import com.jcfk.eam.domain.dto.main.EquStatisticsRepairOrderDTO;
import com.jcfk.eam.domain.po.base.EamRepairOrder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EquStatisticsRepairOrderService extends BaseService<EquStatisticsRepairOrderDao, EamRepairOrder> {

    /**
     * 故障维修数据统计
     */
    public EquStatisticsRepairOrderDTO queryRepairOrderList(EquStatisticsDTO query) {
        EquStatisticsRepairOrderDTO dto = new EquStatisticsRepairOrderDTO();
        dto.setAectionStatistics(this.queryAectionStatistics(query));
        dto.setLocationStatistics(this.queryLocationStatistics(query));
        dto.setTeamStatistics(this.queryTeamStatistics(query));
        dto.setAnnualStatistics(this.queryAnnualStatistics(query));
        dto.setNonStopFault(this.queryNonStopFault(query));
        dto.setStopFault(this.queryStopFault(query));
        return dto;
    }

    /**
     * 故障维修-年度统计(折线)
     */
    private EChartData queryAnnualStatistics(EquStatisticsDTO query) {
        EChartData eChartData = new EChartData();
        List<EquStatisticsDTO> list = this.baseMapper.queryAnnualStatistics(query);
        if(list == null || list.isEmpty()){
            return eChartData;
        }

        list.forEach(item ->{
            eChartData.getXData().add(item.getXData());
            eChartData.getYData().add(item.getYData());
        });
        return eChartData;
    }

    /**
     * 故障维修-工段统计(柱状)
     */
    private EChartData queryAectionStatistics(EquStatisticsDTO query) {
        EChartData eChartData = new EChartData();
        List<EquStatisticsDTO> list = this.baseMapper.queryAectionStatistics(query);
        if(list == null || list.isEmpty()){
            return eChartData;
        }

        list.forEach(item ->{
            eChartData.getXData().add(item.getXData());
            eChartData.getYData().add(item.getYData());
        });
        return eChartData;
    }
    /**
     * 故障维修-班组统计(饼图)
     */
    private EChartData queryTeamStatistics(EquStatisticsDTO query) {
        EChartData eChartData = new EChartData();
        List<EChartDataItem> list = this.baseMapper.queryTeamStatistics(query);
        if(list == null || list.isEmpty()){
            return eChartData;
        }
        eChartData.setData(list);
        return eChartData;
    }
    /**
     * 故障维修-故障位置统计(柱状)
     */
    private List<EChartData> queryLocationStatistics(EquStatisticsDTO query) {
        List<EChartData> listData = new ArrayList<>();
        List<EquStatisticsDTO> list = this.baseMapper.queryLocationStatistics(query);
        if(list == null || list.isEmpty()){
            return listData;
        }

        Map<String, List<EquStatisticsDTO>> groupedItems = list.stream()
                .collect(Collectors.groupingBy(EquStatisticsDTO::getXName));

        groupedItems.forEach((key,value)->{
            EChartData eChartData = new EChartData();
            eChartData.setName(key);
            value.forEach(item ->{
                eChartData.getXData().add(item.getXData());
                eChartData.getYData().add(item.getYData());
            });
            listData.add(eChartData);
         });

        return listData;
    }
    /**
     * 非停机故障(柱状)
     */
    private EChartData queryNonStopFault(EquStatisticsDTO query) {
        EChartData eChartData = new EChartData();
        query.setStatisticsState(2);
        List<EquStatisticsDTO> list = this.baseMapper.queryAectionStatistics(query);
        if(list == null || list.isEmpty()){
            return eChartData;
        }

        list.forEach(item ->{
            eChartData.getXData().add(item.getXData());
            eChartData.getYData().add(item.getYData());
        });
        return eChartData;
    }
    /**
     * 停机故障(柱状)
     */
    private EChartData queryStopFault(EquStatisticsDTO query) {
        EChartData eChartData = new EChartData();
        query.setStatisticsState(1);
        List<EquStatisticsDTO> list = this.baseMapper.queryAectionStatistics(query);
        if(list == null || list.isEmpty()){
            return eChartData;
        }

        list.forEach(item ->{
            eChartData.getXData().add(item.getXData());
            eChartData.getYData().add(item.getYData());
        });
        return eChartData;
    }
}
